package com.yztc.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yztc.chunboapp.MainActivity;
import com.yztc.chunboapp.R;
import com.yztc.ui.LoginActivity;
import com.yztc.ui.MyAccumulationActivity;
import com.yztc.ui.MyChunBoCardActivity;
import com.yztc.ui.MyChunBoTicketActivity;
import com.yztc.ui.MyCollectActivity;
import com.yztc.ui.MyOrdersActivity;
import com.yztc.ui.MyshengYuActivity;
import com.yztc.ui.RegisterActivity;
import com.yztc.ui.ServiceActivity;
import com.yztc.ui.SettingActivity;

/**
 * Created by My on 2016/10/6.
 */
public class MyFragment extends Fragment{
    private View view;
    //登录注册按钮
    private Button mBtnLogin,mBtnRegister;
    //两个布局
    private LinearLayout mLineBefore,mLineAfter;
    //上导航设置和客服ImageButton
    private ImageButton mIBtnSetting,mIBtnService;
    //我的订单等TextView
    private TextView mTVAccumulation,mTVChunBoCard,mTVChunBoTicket,mTVCollect,
    mTVOrders,mTVShengYu;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.framgent_my,null);
        initView();
        //设置监听
        setOnClickListener();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        //判断用户登录状态
        if (MainActivity.tag == true){
            //假如用户已经登录，则隐藏注册、登录按钮布局
            mLineBefore.setVisibility(View.GONE);
            //用户信息布局回复可见
            mLineAfter.setVisibility(View.VISIBLE);
        }else {
            //假如用户未登录，则隐藏用户信息布局
            mLineAfter.setVisibility(View.GONE);
            //注册、登录按钮布局回复可见
            mLineBefore.setVisibility(View.VISIBLE);
        }
    }

    private void setOnClickListener() {


        //点击登录按钮，跳转到登录界面
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
        //点击注册的按钮，跳转到注册界面
        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), RegisterActivity.class);
                startActivity(intent);
            }
        });

        //点击设置按钮，跳转到设置界面
        mIBtnSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
            }
        });

        //判断用户登录状态，是否点击进入客服页面
        mIBtnService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainActivity.tag == true){
                    Intent intent = new Intent(getActivity(), ServiceActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        //判断用户登录状态，并监听 我的订单 TextView
        mTVOrders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainActivity.tag == true){
                    Intent intent = new Intent(getActivity(), MyOrdersActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        //判断用户登录状态，并监听 我的收藏 TextView
       mTVCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainActivity.tag == true){
                    Intent intent = new Intent(getActivity(), MyCollectActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        //判断用户登录状态，并监听 我的积分 TextView
        mTVAccumulation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainActivity.tag == true){
                    Intent intent = new Intent(getActivity(),MyAccumulationActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        //判断用户登录状态，并监听 我的春播券 TextView
        mTVChunBoTicket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainActivity.tag == true){
                    Intent intent = new Intent(getActivity(), MyChunBoTicketActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        //判断用户登录状态，并监听 我的春播卡 TextView
        mTVChunBoCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainActivity.tag == true){
                    Intent intent = new Intent(getActivity(), MyChunBoCardActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        //判断用户登录状态，并监听 我的余额 TextView
        mTVShengYu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainActivity.tag == true){
                    Intent intent = new Intent(getActivity(), MyshengYuActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    private void initView() {
        mBtnLogin = (Button) view.findViewById(R.id.btn_my_login);
        mBtnRegister = (Button) view.findViewById(R.id.btn_my_register);
        mLineAfter = (LinearLayout) view.findViewById(R.id.line_login_after);
        mLineBefore = (LinearLayout) view.findViewById(R.id.line_login_before);
        mIBtnSetting = (ImageButton) view.findViewById(R.id.ibtn_my_setting);
        mIBtnService = (ImageButton) view.findViewById(R.id.ibtn_my_enter);
        mTVOrders = (TextView) view.findViewById(R.id.tv_my_orders);
        mTVCollect = (TextView) view.findViewById(R.id.tv_my_collect);
        mTVAccumulation = (TextView) view.findViewById(R.id.tv_my_accumulation);
        mTVChunBoTicket = (TextView) view.findViewById(R.id.tv_my_chunboticlet);
        mTVChunBoCard = (TextView) view.findViewById(R.id.tv_my_chunbocard);
        mTVShengYu = (TextView) view.findViewById(R.id.tv_my_shengyu);
    }
}
